package com.jinbo.footmessage.collection;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.MyApplication;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.WorkerInformationBean;
import com.jinbo.footmessage.serverInf.ServerInfActivity;
import com.jinbo.footmessage.utils.LocalPreferencesHelper;
import com.jinbo.footmessage.view.TitleBarView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 我的收藏
 * Created by fangjinbo on 2016/3/26.
 */
public class MyCollectionActivity extends BaseActivity implements View.OnClickListener {

    private TitleBarView tbv_titleBar;
    private ListView mList;
    private  CollectionListViewAdapter adapter;


    private ArrayList<WorkerInformationBean> mServersList=new ArrayList<>();
   // private LocalPreferencesHelper localPreferencesHelper;


    //ArrayList<WorkerInformationBean> list;

    private static String COLLECTION_INTENT="COLLECTION_INTENT";
    public static void launch(Activity activity,List<WorkerInformationBean> list) {
        Intent intent = new Intent(activity,MyCollectionActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(COLLECTION_INTENT, (Serializable)list);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection);
      //  EventBusUtil.getInstance().getCommonEventBus().register(this);
       // localPreferencesHelper = new LocalPreferencesHelper(this, LocalPreferencesHelper.DB_NAME);
        String user_id= MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        String idstrs=MyApplication.local.getString(user_id + LocalPreferencesHelper.WORKER_IDS);
        String [] ids=idstrs.split("-");
        ArrayList<WorkerInformationBean> list=(ArrayList<WorkerInformationBean>)getIntent().getSerializableExtra(COLLECTION_INTENT);
        for(int i = 0;i<ids.length;i++) {
            for(int j=0;j<list.size();j++)
             {
                 if(ids[i].equals(list.get(j).id)) {
                     mServersList.add(list.get(j));
                     break;
                 }
            }
        }


        initTitleBar();
        initView();
        //initFooter();
        setListener();
        //initData("init");
    }
/*   public void onEventMainThread(final LoginEvent event) {
        initData("init");
    }*/

    @Override
    protected void onDestroy() {
        super.onDestroy();
       // EventBusUtil.getInstance().getCommonEventBus().unregister(this);
    }
    private void initView() {
        // TODO Auto-generated method stub
        mList = (ListView) findViewById(R.id.listView);
        adapter=new CollectionListViewAdapter(this,mServersList);
        mList.setAdapter(adapter);
  /*      inflater = LayoutInflater.from(this);
        
        dataList = new ArrayList<ProjectsBean>();

        ll_dataNull = (LinearLayout) findViewById(R.id.data_null);
        mllLogOut = (LinearLayout) findViewById(R.id.ll_login_out);
        collection = (Button) findViewById(R.id.collection);
        collection.setOnClickListener(this);
        findViewById(R.id.bt_login).setOnClickListener(this);*/
    }




    // 初始化titleBar
    private void initTitleBar() {
        tbv_titleBar = (TitleBarView) findViewById(R.id.titleBar);
        // 设置标题
        tbv_titleBar.setCenterTitle("我的收藏");
        tbv_titleBar.setLeftBackButtonIsVisibler(true);
        tbv_titleBar.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);

    }

    /**
     * 设置监听事件
     */
    private void setListener() {
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ServerInfActivity.launch(MyCollectionActivity.this,mServersList.get(position));
            }
        });
    }

  


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
    /*        case R.id.collection:
                //---跳转到查看工程页面
                finish();
                break;*/
     /*       case R.id.bt_login:
                LoginActivity.launch(MyCollectionProjectActivity.this, 0);
                break;*/
            default:
                break;
        }
    }

    
}
